package com.xianrenit.xianrenpuzi.manager.controller;

import com.xianrenit.xianrenpuzi.manager.service.SysMenuService;
import com.xianrenit.xianrenpuzi.model.entity.sys.SysMenu;
import com.xianrenit.xianrenpuzi.model.result.Result;
import com.xianrenit.xianrenpuzi.model.vo.sys.SysMenuVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Tag(name = "菜单管理模块")
@RequestMapping("/admin/system/menu")
public class SysMenuController {
    @Resource
    SysMenuService sysMenuService;
    @Operation(summary = "查询当前用户的菜单嵌套列表")
    @GetMapping("menus")
    public Result listMenus(){
        List<SysMenuVo> menuVos = sysMenuService.listMenus();
        return Result.ok(menuVos);
    }
    @Operation(summary = "查询角色已分配菜单id列表")
    @GetMapping("listMenuIdsByRoleId/{roleId}")
    public Result listMenuIdsByRoleId(@PathVariable("roleId")Long roleId){
        List<Long> menuIds = sysMenuService.listMenuIdsByRoleId(roleId);
        return Result.ok(menuIds);
    }
    @Operation(summary = "查询所有菜单嵌套集合")
    @GetMapping("listNestedMenus")
    public Result listNestedMenus(){
        // 1级菜单集合(每个1级菜单中携带自己的子菜单集合)
        List<SysMenu> parents = sysMenuService.listNestedMenus();
        return Result.ok(parents);
    }
    @Operation(summary = "删除指定菜单")
    @DeleteMapping("deleteById/{id}")
    public Result deleteById(@PathVariable("id")Long id){
        sysMenuService.deleteById(id);
        return Result.ok();
    }
    @Operation(summary = "新增菜单")
    @PostMapping("save")
    public Result save(@RequestBody SysMenu sysMenu){
        sysMenuService.save(sysMenu);
        return Result.ok();
    }
    @Operation(summary = "查询指定菜单")
    @GetMapping("getById/{id}")
    public Result getById(@PathVariable("id")Long id){
        SysMenu sysMenu = sysMenuService.getById(id);
        return Result.ok(sysMenu);
    }
    @Operation(summary = "更新菜单")
    @PutMapping("update")
    public Result update(@RequestBody SysMenu sysMenu){
        sysMenuService.update(sysMenu);
        return Result.ok();
    }
}
